<template>
    <div>
        <hd-tab :tabs="tabs"></hd-tab>
        <el-card shadow="never" :body-style="{ padding: '20px' }">
            <div slot="header">
                {{ title }}

                <el-alert
                    :title="`您正在管理【${form.title}】模型`" v-if="this.id"
                    type="info">
                </el-alert>
            </div>
            <el-form :model="form" ref="form" label-width="80px" :inline="false" size="normal">
                <el-form-item label="模型名称">
                    <el-input v-model="form.title"></el-input>
                    <hd-error name="title"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">保存提交</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
const form = {title: '', fields: {}}
import tabs from './tabs'
import fields from '../field/fields'

export default {
    path: false,
    props: ['id'],
    data() {
        return {
            form: Object.assign({}, form),
            tabs,
            title: '',
            fields
        }
    },
    async created() {
        if (this.id) {
            this.title = '编辑文章'
            this.form = await this.axios(`admin/article/${this.id}`)
            if (this.form.fields == null) {
                this.form.fields = this.fields
            }
        }
    },
    methods: {
        async onSubmit() {
            const url = this.id ? `admin/article/${this.id}` : 'admin/article'
            if (!this.id) {
                this.form.fields = fields
            }
            await axios[this.id ? 'put' : 'post'](url, this.form)
            this.$router.push({name: 'admin.article.index'})
        }
    }
}
</script>

<style scoped>

</style>
